<template>
  <div class="statistics">
    <section class="search-content">
      <el-row :gutter="4" type="flex" justify="start">
        <el-col :span="24">
          <el-cascader
            :change-on-select="true"
            :options="courseCategorys"
            v-model="courseCategory"
            @change="handleFilter"
          />
          <el-input
            v-model="title"
            placeholder="标题"
            style="width:150px"
            @keyup.enter.native="handleFilter"
          />
          <el-input
            v-model="creator"
            placeholder="创建者"
            style="width:150px"
            @keyup.enter.native="handleFilter"
          />
          <el-button type="primary" @click="handleFilter">搜索</el-button>
          <el-button size="medium" type="info" @click="clearParams">清空</el-button>
          <el-dropdown>
            <el-button type="primary">
              选择课程
              <i class="el-icon-arrow-down el-icon--right"/>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <div style="padding:10px;">
                <el-radio-group v-model="courseType" @change="handleFilter">
                  <el-radio label="normal">普通课程</el-radio>
                  <el-radio label="live">直播课程</el-radio>
                  <el-radio label="open">公开课</el-radio>
                  <el-radio label="liveOpen">直播公开课</el-radio>
                </el-radio-group>
              </div>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </section>
    <all-table
      v-loading="listLoading"
      :table-label="tableLabel"
      :table-data="tableData.slice((currentpage-1)*pagesize,currentpage*pagesize)"
    >
      <el-table-column slot="first" label="课程名" prop="manage" align="center" min-width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="viewCourse(scope)">{{ scope.row.title }}</el-button>
        </template>
      </el-table-column>
      <el-table-column slot="button" label="操作" prop="manage" align="center" min-width="100">
        <template slot-scope="scope">
          <el-button type="primary" @click="viewTasks(scope)">查看任务</el-button>
        </template>
      </el-table-column>
    </all-table>
    <pagination
      :total="total"
      :current-page="currentpage"
      :page-size="pagesize"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />

    <!-- 查看任务按钮弹窗 -->
    <el-dialog :visible.sync="dialogTableVisible" :title="dialogTitle">
      <el-button type="primary" style="float:right;margin-bottom:20px;">导出课时数据</el-button>
      <all-table :table-label="dialogLabel" :table-data="dialogData.slice((currentpage-1)*pagesize,currentpage*pagesize)"/>
    </el-dialog>
    <!-- 课程名弹窗 -->
    <el-dialog :visible.sync="courseDialog" :title="courseTitle">
      <all-table :table-label="courseLabel" :table-data="courseData.slice((currentpage-1)*pagesize,currentpage*pagesize)"/>
    </el-dialog>
  </div>
</template>

<script>
import allTable from '@/components/Eltable'
import Pagination from '@/components/Pagination'
import { fetchList, getTasks, getStudyUser,  } from '@/api/subject/course'
import { getCategoryList } from '@/api/subject/category'
import { getTenantId } from '@/utils/auth'
export default {
  components: {
    allTable,
    Pagination
  },
  data() {
    return {
      dialogData: [],
      courseData: [],
      total: 0,
      currentpage: 1,
      pagesize: 10,
      listLoading: false,
      dialogTableVisible: false,
      courseDialog: false,
      courseTitle: '',
      dialogTitle: '',
      title: '',
      creator: '',
      courseCategory: [],
      courseCategorys: [
        {
          label: '课程分类',
          value: ''
        }
      ],
      tableLabel: [
        {
          label: '任务数量',
          width: '',
          prop: 'lessonNum'
        },
        {
          label: '学员总数',
          width: '',
          prop: 'studentNum'
        },
        {
          label: '完成课程人数',
          width: '',
          prop: 'completeNum'
        },
        {
          label: '课程学习时长(分)',
          width: '',
          prop: 'duration'
        },
        {
          label: '课程收入(元)',
          width: '',
          prop: 'income'
        }
      ],
      tableData: [],
      courseLabel: [
        {
          label: '用户名',
          width: '',
          prop: 'userName'
        },
        {
          label: '加入课程时间',
          width: '',
          prop: 'createTimeStr'
        },
        {
          label: '已学课时数',
          width: '',
          prop: 'learnedNum'
        },
        {
          label: '课程完成时间',
          width: '',
          prop: 'finishedTimeStr'
        },
        {
          label: '笔记数',
          width: '',
          prop: 'noteNum'
        }
      ],
      dialogLabel: [
        {
          label: '任务名', width: '10', prop: 'title'
        },
        {
          label: '任务类型	', width: '10', prop: 'type'
        },
        {
          label: '时长(分)', width: '15', prop: 'length'
        },
        {
          label: '已学学员数', width: '15', prop: 'learnedNum'
        },
        {
          label: '媒体来源', width: '15', prop: 'mediaSource'
        },
        {
          label: '媒体时长(分)', width: '15', prop: 'videoMinute'
        },
        {
          label: '媒体文件大小', width: '20', prop: 'mediaSize'
        }
      ],
      courseType: 'normal'
    }
  },
  mounted() {
    this.getData();
    this.getCategoryList()
  },
  methods: {
    handleSizeChange(val) {
      this.currentpage = 1;
      this.pagesize = val.value;
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
      this.getData()
    },
    handleCurrentChange(val) {
      this.currentpage = val.value;
      if (this.pagesize >= 20) {
        scrollTo(50, 800)
      }
      this.getData()
    },
    clearParams() {
      this.courseCategory = [];
      this.title = '';
      this.creator = '';
      this.getData()
    },
    handleFilter() {
      this.getData()
    },
    getData() {
      this.listLoading = true;
      const params = {};
      params.page = this.currentpage;
      params.pageSize = this.pagesize;
      params.tenantId = getTenantId();
      params.name = this.title;
      params.userName = this.creator;
      params.courseCategory = this.courseCategory[this.courseCategory.length - 1];
      params.courseType = this.courseType;
      fetchList(params).then(response => {
        this.total = response.data.items.length;
        this.tableData = response.data.items;
        this.listLoading = false
      })
    },
    viewTasks(scope) {
      this.dialogTableVisible = true;
      this.dialogTitle = scope.row.title;
      getTasks(scope.row.id).then(res => {
        res.data.items.map(item => {
          if (item.type === 'tuwen') item.type = '图文';
          if (item.type === 'video') item.type = '视频';
          if (item.type === 'audio') item.type = '音频';
          if (item.type === 'discuss') item.type = '讨论';
          if (item.type === 'doc') item.type = '文档';
          if (item.type === 'live') item.type = '直播';
          if (item.type === 'testpaper') item.type = '试卷';
          if (item.type === 'ppt') item.type = 'PPT';
          if (item.mediaSource === 'self') item.type = '本站上传';
          if (item.mediaSource === 'youku') item.type = '优酷'
        });
        this.dialogData = res.data.items
      })
    },
    viewCourse(scope) {
      this.courseDialog = true;
      this.courseTitle = scope.row.title;
      getStudyUser(scope.row.id).then(res => {
        this.courseData = res.data.items
      })
    },
    getCategoryList() {
      getCategoryList('course', getTenantId(),'0').then(response => {
        response.data.items.forEach(item => {
          item.label = item.name;
          item.value = item.name;
          if (item.parentId === 0) {
            item.children = [];
            this.courseCategorys.push(item)
          }
        });
        response.data.items.forEach(item => {
          if (item.parentId !== 0) {
            item.label = item.name;
            item.value = item.name;
            this.courseCategorys.forEach(category => {
              if (item.parentId === category.id) {
                category.children.push(item)
              }
            })
          }
        })
      })
    }
  }
}
</script>

<style lang="scss">
.statistics {
  .cell {
    div {
      font-size: 14px;
      color: #606266;
    }
  }
}
</style>
